package com.gl.saasservice.sys.controller;

import com.gl.framework.interceptor.annotation.RepeatSubmit;
import com.gl.framework.web.response.Result;
import com.gl.saasservice.sys.service.DepartmentService;
import com.gl.saasservice.sys.service.RoleService;
import com.gl.saasservice.sys.vo.DepartmentHierarchyVO;
import com.gl.saasservice.sys.vo.DepartmentVO;
import com.gl.saasservice.sys.vo.RolePermissionRequestVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping(path = "/department")
public class DepartmentController {
    private static final Logger LOG = LoggerFactory.getLogger(DepartmentController.class);

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private RoleService roleService;

    /**
     * 机构层级列表
     *
     * @param token
     * @returnt
     */
    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    @ResponseBody
    public Result page(@RequestHeader String token,
                       DepartmentVO filter) {

        try {
            Map<String, Object> dataMap = departmentService.pages(token, filter);
            return Result.success(dataMap.get("data"));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return Result.fail();
        }
    }

    /**
     * 添加机构层级信息
     *
     * @param token
     * @param departmentVO
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    @RepeatSubmit
    public Result add(@RequestHeader String token,
                      @RequestBody DepartmentVO departmentVO) {
        return departmentService.save(token, departmentVO);
    }

    /**
     * 更新机构层级信息
     *
     * @param token
     * @param departmentVO
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@RequestHeader String token,
                         @RequestBody DepartmentVO departmentVO) {
        return departmentService.save(token, departmentVO);
    }

    /**
     * 添加一个机构层级下级节点
     *
     * @param token
     * @param departmentVO
     * @return
     */
    @RequestMapping(value = "/addNode", method = RequestMethod.POST)
    @ResponseBody
    public Result addNode(@RequestHeader String token,
                          @RequestBody DepartmentVO departmentVO) {
        return departmentService.save(token, departmentVO);
    }

    /**
     * 机构层级-限制人数
     */
    @PostMapping(value = "/departmentLimitPeople")
    @ResponseBody
    public Result departmentLimitPeople(@RequestHeader String token, @RequestParam(name = "depId", required = false) Long depId, @RequestParam(name = "maxNum", required = false) Integer maxNum) {
        return departmentService.departmentLimitPeople(token, depId, maxNum);
    }

    /**
     * 机构层级-获取角色授权列表
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/permissionList/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public Result permissionList(@RequestHeader String token,
                                 @PathVariable Long roleId) {
        return departmentService.permissionList(token, roleId);
    }

    /**
     * 机构层级-授权角色权限
     *
     * @param roleId
     * @param vo
     * @author
     */
    @PostMapping(value = "/permissionSave/{roleId}")
    @ResponseBody
    public Result permissionSave(@RequestHeader String token,
                                 @PathVariable Long roleId,
                                 @RequestBody RolePermissionRequestVO vo) {
        return roleService.saveRolePermission(token, roleId, vo.getPermissionIds());
    }

    /**
     * 删除机构层级信息
     *
     * @param token
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@RequestHeader String token,
                         @PathVariable Long id) {
        return departmentService.delete(token, id);
    }

    /**
     * 机构层级-修改状态
     *
     * @param filter
     * @return
     */
    @RequestMapping(path = "/updateStatus", method = RequestMethod.PUT)
    @ResponseBody
    public Result updateStatus(@RequestHeader String token,
                               @RequestBody DepartmentHierarchyVO filter) {
        return departmentService.UpdateStatus(token, filter);
    }

    /**
     * 机构层级-默认流程列表
     */
    @RequestMapping(path = "/defaultProcessList", method = RequestMethod.GET)
    @ResponseBody
    public Result defaultProcessList(@RequestHeader String token,
                                     Long id) {

        return departmentService.defaultProcessList(token, id);
    }

    /**
     * 区域标签列表
     */
    @RequestMapping(path = "/departmentTag", method = RequestMethod.GET)
    @ResponseBody
    public Result departmentTag(@RequestHeader String token) {
        return departmentService.departmentTag(token);
    }

    /**
     * 企业标签列表
     */
    @RequestMapping(path = "/departmentEnterpriseTag", method = RequestMethod.GET)
    @ResponseBody
    public Result departmentEnterpriseTag(@RequestHeader String token) {
        return departmentService.departmentEnterpriseTag(token);
    }

    /**
     * 机构层级-修改收款方式状态：0平台商户号收款 1企业商户号收款
     *
     * @param filter
     * @return
     */
    @RequestMapping(path = "/updateMerchantsTypeStatus", method = RequestMethod.PUT)
    @ResponseBody
    public Result updateMerchantsTypeStatus(@RequestHeader String token,
                                            @RequestBody DepartmentHierarchyVO filter) {
        return departmentService.updateMerchantsTypeStatus(token, filter);
    }

    /**
     * 机构层级-修改公租房审批状态：0需要审批 1不用审批
     *
     * @param filter
     * @return
     */
    @RequestMapping(path = "/updateBindAuitStatus", method = RequestMethod.PUT)
    @ResponseBody
    public Result updateBindAuitStatus(@RequestHeader String token,
                                       @RequestBody DepartmentHierarchyVO filter) {
        return departmentService.updateBindAuitStatus(token, filter);
    }


    /***********************部门*************************/

    /**
     * 部门列表
     *
     * @param token
     * @returnt
     */
    @RequestMapping(value = "/departmentPages", method = RequestMethod.GET)
    @ResponseBody
    public Result departmentPages(@RequestHeader String token,
                                  DepartmentVO filter) {

        return departmentService.departmentPages(token, filter);

    }

    /**
     * 新增子部门
     */
    @PostMapping(value = "/department")
    @ResponseBody
    public Result addChildDepartment(@RequestHeader String token, @RequestBody DepartmentVO departmentVO) {
        return departmentService.addChildDepartment(token, departmentVO);
    }

    /**
     * 编辑子部门
     */
    @PutMapping(value = "/department")
    @ResponseBody
    public Result updateDepartment(@RequestHeader String token, @RequestBody DepartmentVO departmentVO) {
        return departmentService.updateDepartment(token, departmentVO);
    }

    /**
     * 删除子部门
     */
    @DeleteMapping(value = "/department")
    @ResponseBody
    public Result deleteDepartment(@RequestHeader String token, @RequestParam(name = "depId", required = false) Long depId) {
        return departmentService.deleteDepartment(token, depId);
    }

}
